/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ventanas;

import controlador.CtrlVista;
import excepciones.NonexistentEntityException;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import negocio.Sala;
import negocio.Seccion;

/**
 *
 * @author diego
 */
public class FrmSeccion extends javax.swing.JDialog {

    /**
     * Creates new form FrmSeccion
     */
    private CtrlVista vista;// = new CtrlVista();
    private Sala unaSala;
    private Seccion unaSeccion;
    DefaultListModel modelSalas = new DefaultListModel();
    DefaultListModel modelSecciones = new DefaultListModel();
    
    public FrmSeccion(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        try{
            this.vista = new CtrlVista();
        }
        catch(Exception e){            
            JOptionPane.showMessageDialog(this, "Error de Inicializacion");
        }
        this.inicializarContoles();
    }
    public void inicializarContoles(){
        this.lstSalas.setModel(modelSalas);
        this.lstSecciones.setModel(this.modelSecciones);
        this.cargarSalas();
        this.inicializarBotones();
    }
    
    public void cargarSalas(){
        if(this.vista.obtenerSalasActivas().size() > 0){
            for (Sala unaSala : this.vista.obtenerSalasActivas()) {
                this.modelSalas.addElement(unaSala);
            }
        }else{
            this.modelSalas.addElement("No se encontraron Salas");
        }
    }
    public void seleccionarSala(){
        if(this.vista.obtenerSalasActivas().size() > 0 ){
            this.unaSala = (Sala) this.modelSalas.getElementAt(this.lstSalas.getSelectedIndex());            
        }
        
    }
    
    public void cargarSecciones(){
        this.modelSecciones.clear();
        
        if(this.unaSala != null && this.unaSala.ObtenerSecciones().size() > 0){
            for (Seccion miSeccion : this.unaSala.ObtenerSecciones()) {                
                this.modelSecciones.addElement(miSeccion);
            }            
        }else{
            this.modelSecciones.addElement("No se encontraron secciones");
        }
        
    }
    
    public void seleccionarSeccion(){
        if(this.modelSecciones.size() > 0){
            this.unaSeccion = (Seccion) this.modelSecciones.getElementAt(this.lstSecciones.getSelectedIndex());
            this.txtNombreSeccion.setText(this.unaSeccion.getCodigo());
            if(this.unaSeccion.isEstado()){
                this.rdActiva.setSelected(true);
                this.rdInactiva.setSelected(false);
            }else{
                this.rdActiva.setSelected(false);
                this.rdInactiva.setSelected(true);
            }
            this.btnNueva.setEnabled(false);
            this.btnGuardar.setEnabled(true);
            this.btnBorrar.setEnabled(true);
            this.btnEditar.setEnabled(true);
        }
        
    }
    
    public void nuevoRegistro(){
        this.btnGuardar.setEnabled(true);
        this.btnNueva.setEnabled(false);
        this.btnBorrar.setEnabled(false);
        this.btnEditar.setEnabled(false);
        this.txtNombreSeccion.setEnabled(true);
        this.txtNombreSeccion.setText("");
        this.rdActiva.setSelected(true);
        this.rdInactiva.setSelected(false);
        this.rdActiva.setEnabled(true);
        this.rdInactiva.setEnabled(true);
        
        this.unaSeccion = null;

        
    }
    
    public void guardarRegistro() {
        String nombre = this.txtNombreSeccion.getText().trim();            
        if(!nombre.isEmpty()){
            if(this.unaSeccion == null){                
                try{
                   this.unaSala.agregarSeccion(this.txtNombreSeccion.getText());                       
                    
                }
                catch(Exception ex){
                    JOptionPane.showMessageDialog(this, ex.getMessage()); 
                }

            }else{
                try{
                    this.unaSala.modificarSeccion(this.unaSeccion);
                }
                catch(NonexistentEntityException ex){
                    JOptionPane.showMessageDialog(this, ex.getMessage()); 
                }
                catch(Exception ex){
                    JOptionPane.showMessageDialog(this, ex.getMessage()); 
                }
            }
            this.inicializarBotones();
        }
    }
    
    public void inicializarBotones(){
        this.cargarSecciones();
        this.txtNombreSeccion.setText("");
        this.txtNombreSeccion.setEnabled(false);
        this.btnBorrar.setEnabled(false);
        this.btnEditar.setEnabled(false);
        this.btnGuardar.setEnabled(false);
        this.btnNueva.setEnabled(true);
        this.rdActiva.setEnabled(false);
        this.rdInactiva.setEnabled(false);
    }
    public void editar(){
        this.txtNombreSeccion.setEnabled(true);
        this.rdActiva.setEnabled(true);
        this.rdInactiva.setEnabled(true);
        this.btnBorrar.setEnabled(false);
        this.btnEditar.setEnabled(false);
        this.btnGuardar.setEnabled(true);
        this.btnNueva.setEnabled(false);
    }
    public void borrar(){
       if(this.unaSala != null){
        int respuesta = JOptionPane.showConfirmDialog(null, "¿Esta accion deshablitara la seccion?", "Confirmación", JOptionPane.YES_NO_OPTION);
        switch(respuesta) {
            case JOptionPane.YES_OPTION:
                //--- Operaciones en caso afirmativo
                this.unaSeccion.setEstado(false);
                this.rdInactiva.setSelected(true);
                this.rdActiva.setSelected(false);
                this.guardarRegistro();
                break;
        }     
       } 
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        lstSalas = new javax.swing.JList();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        lstSecciones = new javax.swing.JList();
        jLabel3 = new javax.swing.JLabel();
        txtNombreSeccion = new javax.swing.JTextField();
        rdActiva = new javax.swing.JRadioButton();
        rdInactiva = new javax.swing.JRadioButton();
        btnGuardar = new javax.swing.JButton();
        btnNueva = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        btnEditar = new javax.swing.JButton();
        btnBorrar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setText("Salas");

        lstSalas.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        lstSalas.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lstSalasMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(lstSalas);

        jLabel2.setText("Secciones");

        lstSecciones.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        lstSecciones.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lstSeccionesMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(lstSecciones);

        jLabel3.setText("Nombre");

        rdActiva.setText("Activa");
        rdActiva.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                rdActivaMouseClicked(evt);
            }
        });

        rdInactiva.setText("Inactiva");
        rdInactiva.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                rdInactivaMouseClicked(evt);
            }
        });

        btnGuardar.setText("Guardar");
        btnGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGuardarActionPerformed(evt);
            }
        });

        btnNueva.setText("Nueva");
        btnNueva.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNuevaActionPerformed(evt);
            }
        });

        jButton1.setText("Aceptar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        btnEditar.setText("Editar");
        btnEditar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditarActionPerformed(evt);
            }
        });

        btnBorrar.setText("Borrar");
        btnBorrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBorrarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 206, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(45, 45, 45)
                                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 202, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(31, 31, 31)
                                .addComponent(jLabel2)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(btnNueva, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(rdActiva, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel3))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(rdInactiva)
                                .addGap(0, 0, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(btnGuardar)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnBorrar, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(btnEditar, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(txtNombreSeccion, javax.swing.GroupLayout.Alignment.TRAILING)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 681, Short.MAX_VALUE)
                        .addComponent(jButton1)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(13, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 194, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2))
                        .addGap(2, 2, 2)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel3)
                                    .addComponent(txtNombreSeccion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(rdActiva)
                                    .addComponent(rdInactiva))
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(btnNueva)
                                    .addComponent(btnGuardar)
                                    .addComponent(btnBorrar)
                                    .addComponent(btnEditar)))
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 192, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 14, Short.MAX_VALUE)
                .addComponent(jButton1)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void lstSalasMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lstSalasMouseClicked
        // TODO add your handling code here:
        this.seleccionarSala();
        this.cargarSecciones();
    }//GEN-LAST:event_lstSalasMouseClicked

    private void lstSeccionesMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lstSeccionesMouseClicked
        // TODO add your handling code here:
        this.seleccionarSeccion();
    }//GEN-LAST:event_lstSeccionesMouseClicked

    private void btnNuevaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNuevaActionPerformed
        // TODO add your handling code here:
        this.nuevoRegistro();
    }//GEN-LAST:event_btnNuevaActionPerformed

    private void btnGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGuardarActionPerformed
        // TODO add your handling code here:
        this.guardarRegistro();
    }//GEN-LAST:event_btnGuardarActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void btnEditarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditarActionPerformed
        // TODO add your handling code here:
        this.editar();
    }//GEN-LAST:event_btnEditarActionPerformed

    private void btnBorrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBorrarActionPerformed
        // TODO add your handling code here:
        this.borrar();
    }//GEN-LAST:event_btnBorrarActionPerformed

    private void rdActivaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_rdActivaMouseClicked
        // TODO add your handling code here:
        this.rdInactiva.setSelected(false);
    }//GEN-LAST:event_rdActivaMouseClicked

    private void rdInactivaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_rdInactivaMouseClicked
        // TODO add your handling code here:
        this.rdActiva.setSelected(false);
    }//GEN-LAST:event_rdInactivaMouseClicked

    /**
     * @param args the command line arguments
     */
    public static void main2(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(FrmSeccion.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FrmSeccion.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FrmSeccion.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FrmSeccion.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                FrmSeccion dialog = new FrmSeccion(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBorrar;
    private javax.swing.JButton btnEditar;
    private javax.swing.JButton btnGuardar;
    private javax.swing.JButton btnNueva;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JList lstSalas;
    private javax.swing.JList lstSecciones;
    private javax.swing.JRadioButton rdActiva;
    private javax.swing.JRadioButton rdInactiva;
    private javax.swing.JTextField txtNombreSeccion;
    // End of variables declaration//GEN-END:variables
}
